package cn.zwx.member.svc.rpc;

import cn.zwx.base.entity.ResponseEntity;
import cn.zwx.base.exception.BizWrapException;
import cn.zwx.base.utils.ValidationUtils;
import cn.zwx.member.svc.domain.entity.DistrictsEntity;
import cn.zwx.member.svc.rpc.dto.DsitrictsListDTO;
import cn.zwx.member.svc.rpc.facade.DubboDsitrictsService;
import cn.zwx.member.svc.rpc.vo.DsitrictsListVO;
import cn.zwx.member.svc.service.DistrictsService;
import org.apache.dubbo.apidocs.annotations.ApiDoc;
import org.apache.dubbo.apidocs.annotations.ApiModule;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangwenxue
 * ApiModule 必要 api-doc会查找所有带有@ApiModule的bean对象
 */
@Service
@ApiModule(value = "获取相关行政区域接口", apiInterface = DubboDsitrictsService.class)
@DubboService(version = "v1.0.0",interfaceClass = DubboDsitrictsService.class,timeout = 8000)
public class DubboDsitrictsServiceImpl implements DubboDsitrictsService {

    @Resource
    DistrictsService districtsService;

    @Override
    @ApiDoc(value = "查询下级行政区域接口")
    public ResponseEntity<List<DsitrictsListVO>> getDistrictsList(DsitrictsListDTO dto) throws BizWrapException {
        // 1.验证参数
        ValidationUtils.getInstance().validate(dto);
        // 2.query dsitricts list
        final List<DistrictsEntity> districtsList = districtsService.getDistrictsList(dto.getDistrictsId());
        // 3.return obj ...
        if (CollectionUtils.isEmpty(districtsList)){
            return ResponseEntity.success();
        }
        List<DsitrictsListVO> returnList = new ArrayList<>();
        districtsList.forEach(t->{
            final DsitrictsListVO dsitrictsListVO = new DsitrictsListVO();
            BeanUtils.copyProperties(t,dsitrictsListVO);
            returnList.add(dsitrictsListVO);
        });
        return ResponseEntity.success(returnList);
    }
}
